package com.applepaihs.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.applepaihs.domain.dto.PageDto;
import com.applepaihs.domain.query.RedropesettleQuery;
import com.applepaihs.domain.vo.RedropesettleVo;
import com.applepaihs.mapper.RedropesettleMapper;
import com.applepaihs.model.Redropesettle;
import com.applepaihs.service.RedropesettleService;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
public class RedropesettleServiceImpl extends ServiceImpl<RedropesettleMapper, Redropesettle> implements RedropesettleService {

    @Override
    @Transactional
    public void deductionPrice(Long id, Integer money) {
        Redropesettle redropesettle = getById(id);
        if(redropesettle==null || redropesettle.getStatus()!=1){
            throw new RuntimeException("结算单状态异常！");
        }
        if(redropesettle.getMyPrice()<money){
            throw new RuntimeException("余额不足！");
        }

//        baseMapper.deductionPrice(id,money);
        int remainPrice = redropesettle.getMyPrice() - money;
        lambdaUpdate()
                .set(Redropesettle::getMyPrice,remainPrice)
                .set(remainPrice<=0,Redropesettle::getStatus,2)
                .eq(Redropesettle::getId,id)
                .eq(Redropesettle::getMyPrice,redropesettle.getMyPrice())      //乐观锁
                .update();
    }

    @Override
    public List<Redropesettle> queryRedropesettles(String settleMonth, Short roleType, Long actorWeixinuserId, Integer flowNum) {
        return lambdaQuery()
                .like(settleMonth!=null,Redropesettle::getSettleMonth,settleMonth)
                .eq(roleType!=null,Redropesettle::getRoleType,roleType)
                .eq(actorWeixinuserId!=null,Redropesettle::getActorWeixinuserId,actorWeixinuserId)
                .eq(flowNum!=null,Redropesettle::getFlowNum,flowNum)
                .list();
    }

    @Override
    public PageDto<RedropesettleVo> queryRedropesettlesPage(RedropesettleQuery query) {
        String settleMonth = query.getSettleMonth();
        Short roleType = query.getRoleType();
        Long actorWeixinuserId = query.getActorWeixinuserId();
        Integer flowNum = query.getFlowNum();
        //1.构建分页条件
        //1.1分页条件
        Page<Redropesettle> page = query.toMpPageDefaultSortByGmtModified();


        //2.分页查询
        Page<Redropesettle> p = lambdaQuery()
                .like(settleMonth!=null,Redropesettle::getSettleMonth,settleMonth)
                .eq(roleType!=null,Redropesettle::getRoleType,roleType)
                .eq(actorWeixinuserId!=null,Redropesettle::getActorWeixinuserId,actorWeixinuserId)
                .eq(flowNum!=null,Redropesettle::getFlowNum,flowNum)
                .page(page);
        //3.封装vo结果
//        return PageDto.of(page,RedropesettleVo.class);
        return PageDto.of(page,redropesettle -> {
            RedropesettleVo vo = BeanUtil.copyProperties(redropesettle,RedropesettleVo.class);
            vo.setPic("");//做特殊处理
            return vo;
        });
    }
}
